package com.htu.courseService.auth.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.htu.courseService.auth.strategy.LoginFactory;
import com.htu.courseService.auth.vo.LoginRequest;
import com.htu.courseService.common.enums.PermissionPrefix;
import com.htu.courseService.common.enums.RedisCachePrefix;
import com.htu.courseService.common.util.RedisCache;
import com.htu.courseService.common.util.SymmetricCryptoUtil;
import com.htu.courseService.common.vo.R;
import com.htu.courseService.user.vo.UserInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


/**
 * Created By JNNarrator on 2025/1/1 14:47
 * email: jiangn09@chinatelecom.cn
 */
@RestController
@RequiredArgsConstructor
public class LoginController implements RedisCachePrefix, PermissionPrefix {
	private final LoginFactory loginFactory;
	private final RedisCache redisCache;
	
	@PostMapping("/login")
	public R<?> login(@RequestBody LoginRequest loginRequest){

		return  R.success(loginFactory.getLoginStrategy(loginRequest.getLoginType()).login(loginRequest));
	}
	@GetMapping("/test")
	public R<?> test(){
		StpUtil.checkPermission(USER_CREATE);
		String encode = SymmetricCryptoUtil.encode("123456");
		System.out.println(encode);
		UserInfo userInfo = new UserInfo();
		userInfo.setUsername("wm");
		userInfo.setPassword(encode);
		HashMap<String, List<String>> role = new HashMap<>();
		ArrayList<String> permission = new ArrayList<>();
		permission.add("create");
		role.put("admin",permission);
		userInfo.setRolesAndPermissions(role);
		redisCache.setCacheObject(USER_INFO+"wm",userInfo);
		return R.success();
	}
}
